<?php
class Frame_Model_App extends model {
    
    const READY = 0;
    const OPEN  = 1;
    const TEST  = 2;
    const FAIL  = 3;

    private $sqls = array(
        'get_apps_by_team_id' => 'select `app_id`,`app_name`,`app_has_wap` from `mini_core_app` where `app_id` in(select `app_id` from `mini_core_team_link_app` where `team_id` = [@team_id]);',
        'get_apps_by_user_id' => 'select `app_id`,`app_name`,`app_has_wap` from `mini_core_app` where `app_id` in(select `app_id` from `mini_core_user_link_app` where `user_id` = [@user_id]);',
        'get_opened_apps' => 'select `app_id`,`app_name`,`app_has_wap` from `mini_core_app` where `app_status` = 1;',
        'add_user_category' => 'insert into `mini_core_user_category`(`uc_id`,`uc_name`,`user_id`,`created`) values([@uc_id],[@uc_name],[@user_id],[@created]);',
        'get_user_category' => 'select `uc_id`,`uc_name` from `mini_core_user_category` where `user_id` = [@user_id] order by created desc;',
        'get_by_id_open' => 'select * from `mini_core_app` where `app_id` = [@app_id] and app_status = 1;',
    );

    private $app_status = array(
        self::READY   => '待审核',
        self::OPEN    => '已通过审核',
        self::TEST    => '审核过程中',
        self::FAIL    => '审核未通过',
    );

    //app_category的id采用md5('app_category_name'.order)
    private $app_category_by_order = array(
        array(
            'ac_id'     => '0B487C73A98BD028323E044FCDB76AFC',
            'ac_name'   => '微ERP',
            'ac_icon'   => '',
        ),
        array(
            'ac_id'     => '0CAFFB6C09BE17C9FD3A36DE220D1C92',
            'ac_name'   => '微CRM',
            'ac_icon'   => '',
        ),
        array(
            'ac_id'     => '116F5D7939F93F7C7BC9A75BBAB77348',
            'ac_name'   => '办公协作',
            'ac_icon'   => '',
        ),
        array(
            'ac_id'     => 'ACBDF59C283AA581C04E789B41C2B391',
            'ac_name'   => '文档管理',
            'ac_icon'   => '',
        ),
        array(
            'ac_id'     => 'FF21E7D2152C7B4C39FB86BBD046C914',
            'ac_name'   => '通讯录',
            'ac_icon'   => '',
        ),
        array(
            'ac_id'     => 'DF129FD2772E3AE34AE3E570A7535276',
            'ac_name'   => '人力资源',
            'ac_icon'   => '',
        ),
        array(
            'ac_id'     => 'BBDE1976AE7263D07B297BD58A09AD8D',
            'ac_name'   => '教学培训',
            'ac_icon'   => '',
        ),
        array(
            'ac_id'     => '22F038B5FB6BD125D86654625296C5B9',
            'ac_name'   => '销售管理',
            'ac_icon'   => '',
        ),
        array(
            'ac_id'     => '4958C8252D819793757C570483D69C97',
            'ac_name'   => '商业智能',
            'ac_icon'   => '',
        ),
    );
    private $app_category_by_id = array(
        '0B487C73A98BD028323E044FCDB76AFC'  => array(
            'ac_name'   => '微ERP',
            'ac_icon'   => '',
        ),
        '0CAFFB6C09BE17C9FD3A36DE220D1C92'  => array(
            'ac_name'   => '微CRM',
            'ac_icon'   => '',
        ),
        '116F5D7939F93F7C7BC9A75BBAB77348'  => array(
            'ac_name'   => '办公协作',
            'ac_icon'   => '',
        ),
        'ACBDF59C283AA581C04E789B41C2B391'  => array(
            'ac_name'   => '文档管理',
            'ac_icon'   => '',
        ),
        'FF21E7D2152C7B4C39FB86BBD046C914'  => array(
            'ac_name'   => '通讯录',
            'ac_icon'   => '',
        ),
        'DF129FD2772E3AE34AE3E570A7535276'  => array(
            'ac_name'   => '人力资源',
            'ac_icon'   => '',
        ),
        'BBDE1976AE7263D07B297BD58A09AD8D'  => array(
            'ac_name'   => '教学培训',
            'ac_icon'   => '',
        ),
        '22F038B5FB6BD125D86654625296C5B9'  => array(
            'ac_name'   => '销售管理',
            'ac_icon'   => '',
        ),
        '4958C8252D819793757C570483D69C97'  => array(
            'ac_name'   => '商业智能',
            'ac_icon'   => '',
        ),
    );
    
    public function __construct(){
        parent::__construct();
    }
    
    //根据team_id获取app
    public function get_apps_by_team_id($team_id){
        return mysql::fetch('team_link_app', $this->sqls['get_apps_by_team_id'], array('team_id'=>$team_id));
    }

    //根据user_id获取APP
    public function get_apps_by_user_id($user_id){
        return mysql::fetch('team_link_app', $this->sqls['get_apps_by_user_id'], array('user_id'=>$user_id));
    }
    //获取所有已经开放的应用
    public function get_opened_apps(){
        return mysql::fetch('team_link_app', $this->sqls['get_opened_apps']);
    }

    //根据id获得APP
    public function get_by_id_open($app_id){
        $app = mysql::fetch('app', $this->sqls['get_by_id_open'], array('app_id'=>$app_id));
        if(!empty($app))
            return array_shift($app);
        else
            return false;
    }
    
    //创建用户自定义分类
    public function add_user_category($ucdata){
        $ucdata['created'] = time();
        $ucdata['uc_id'] = md5($ucdata['uc_name'].$ucdata['created'].rand());
        mysql::execute('user_category', $this->sqls['add_user_category'], $ucdata);
        return $ucdata['uc_id'];
    }

    //获取用户自定义分类
    public function get_user_category($user_id){
        return mysql::fetch('user_category', $this->sqls['get_user_category'], array('user_id'=>$user_id));
    }
    
    //获得应用分类列表
    public function get_app_category(){
        return $this->app_category_by_order;
    }

    //获取分类列表以ID为KEY
    public function get_app_category_by_id($ac_id){
        if(isset($this->app_category_by_id[$ac_id]))
            return $this->app_category_by_id[$ac_id];
        else
            return false;
    }
}